﻿using Entity.Customer;
using Entity.Request;
using Hotel.Models;
using IHotelBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Hotel.Controllers.Account
{
    public class AccountController : Controller
    {
        IUserInfoBLL _userInfoBLL;
        public AccountController(IUserInfoBLL userInfoBLL)
        {
            _userInfoBLL = userInfoBLL;
        }
        ResultModel res = new();
        //登录接口
        public IActionResult Login(UserLogin login)
        {
            if(login.UserName == "login")
            {
                res.Data = "/EmployeeView/Login";
                res.IsSuccess = true;
                return Json(res);
            }
            if(login.UserName is null)
            {
                res.Msg = "用户名不能为空";
                return Json(res);
            }
            if(login.Password is null)
            {
                res.Msg = "密码不能为空";
                return Json(res);
            }
            var user = _userInfoBLL.Login(login,out string msg);
            res.Msg = msg;
            if (user != null)
            {
                res.IsSuccess = true;
                HttpContext.Session.SetString("userId", user.Id);
                HttpContext.Session.SetString("userName", login.UserName);
                HttpContext.Response.Cookies.Append("UserId",user.Id);
                HttpContext.Response.Cookies.Append("UserName", user.Name);
                HttpContext.Response.Cookies.Append("Phone", user.Phone);
                HttpContext.Response.Cookies.Append("ID_Card", user.ID_Card);
                res.Data = "/Home/Index";
            }
            return Json(res);
        }
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public IActionResult Register(CustomerInfo customer)
        {
            if(customer.Name is null)
            {
                res.Msg = "姓名不能为空";
                return Json(res);
            }
            if(customer.ID_Card is null)
            {
                res.Msg = "身份证不能为空";
                return Json(res);
            }   
            if(customer.Phone is null)
            {
                res.Msg = "手机号码不能为空";
                return Json(res);
            }
            if(customer.PassWord is null)
            {
                res.Msg = "密码不能为空";
                return Json(res);
            }
            res.IsSuccess = _userInfoBLL.Register(customer, out string msg);
            res.Msg = msg;
            if (res.IsSuccess){
                res.Data = "/Home/Login";
            }
            return Json(res);
        }
        public IActionResult UpdateUserInfo(CustomerInfo customer)
        {
            if (customer.Name is null)
            {
                res.Msg = "姓名不能为空";
                return Json(res);
            }
            if (customer.ID_Card is null)
            {
                res.Msg = "身份证不能为空";
                return Json(res);
            }
            if (customer.Phone is null)
            {
                res.Msg = "手机号码不能为空";
                return Json(res);
            }
            if (customer.PassWord is null)
            {
                res.Msg = "密码不能为空";
                return Json(res);
            }
            res.IsSuccess = _userInfoBLL.UpdateUserInfo(customer, out string msg);
            res.Msg = msg;
            return Json(res);
        }
        public IActionResult DeleteUserInfo(string id)
        {
            res.IsSuccess = _userInfoBLL.DeleteUserInfo(id,out string msg);
            res.Msg = msg;
            return Json(res);
        }


        /// <summary>
        /// 查询用户详情信息
        /// </summary>
        /// <returns></returns>
        public IActionResult GetUserInfoDetail()
        {
            //返回对象
            ResultModel res = new ResultModel();
            string userId = HttpContext.Session.GetString("Id");
            if (string.IsNullOrEmpty(userId))
            {
                res.Msg = "未获取到当前用户Id";
                return Json(res);
            }

            //查询用户详情信息
            var data = _userInfoBLL.GetUserInfoDetail(userId);
            if (data == null)
            {
                res.Msg = "未获取到当前用户信息";
                return Json(res);
            }

            res.IsSuccess = true;
            res.Status = 1;
            res.Data = data;
            return Json(res);
        }

    }
}
